package pacBiopackage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Writer;

import tools.kmer.KmerSet_binary;
import tools.kmer.kmerFunctions;

public class PacBioCorrectionByKmersMain 
{
	public static void main(String[] args) 
	{
		GetOpt go = new GetOpt(args, "o:k:s:a:hf:m:");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		String filenameAlignment = "";   
		String filenameOutput="output.txt";
		String filenameStat = "stat.txt";
		String filenameKmerObject = "";
		int freq_mode=0;
		String freq_table="";
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		    else if ((char)ch == 'a') {
		    	filenameAlignment = go.optArgGet();
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='s')
		    {
		    	filenameStat=go.optArgGet();
		    }
		    else if((char)ch =='k')
		    {
		    	filenameKmerObject=go.optArgGet();
		    }
		    else if((char)ch =='m')
		    {
		    	freq_mode=Integer.valueOf(go.optArgGet());
		    }
		    else if((char)ch == 'f')
		    {
		    	freq_table= go.optArgGet();
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameKmerObject.isEmpty() || filenameStat.isEmpty() || filenameAlignment.isEmpty()) {
		    System.out.println("Usage: -a PacBio -k Kmer [ -o outputFile -s statistic -h -m mode]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-a " + filenameAlignment); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("-s " + filenameStat); //NOI18N
		System.out.println("-k " + filenameKmerObject); //NOI18N
		System.out.println("-m " + freq_mode);
		System.out.println("------------------------");
		
		
		//first we have to read the kmersFromthe file
		KmerSet_binary ks = null;
		BufferedReader in;
		try 
		{
			in = new BufferedReader(new FileReader(filenameKmerObject));
			String kmer;
			System.out.println("reading kmers");
			while(in.ready())
			{
				kmer=in.readLine();
				if(ks !=null)
				{
					 ks.addKmer(kmerFunctions.kmerToUse(kmer.split("\t")[1]),Integer.valueOf(kmer.split("\t")[0])); //already checked for revComp....
				}
				else
				{
					ks= new KmerSet_binary(kmer.split("\t")[1].length());
					ks.addKmer(kmerFunctions.kmerToUse(kmer.split("\t")[1]),Integer.valueOf(kmer.split("\t")[0]));
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
			
		System.out.println("reading kmers done");
//		ObjectSerializing<KmerSet_binary> objectKmer = new ObjectSerializing<KmerSet_binary>();
//		objectKmer.deserializeObject(ks, filenameKmerObject);
		PrintWriter pw = null;
		// now we have to correct each 
		BufferedReader br = null;
		int counter=0;
		try
		{

			Writer fw = new FileWriter(filenameOutput);
			Writer bw = new BufferedWriter(fw);
			pw = new PrintWriter(bw);
			br = new BufferedReader(new FileReader(filenameAlignment));
			
			PacBioErrorStatisticsContainer stat = new PacBioErrorStatisticsContainer();
			PacBioCorrectionByKmers kmerCorrection = new PacBioCorrectionByKmers(ks,stat,freq_mode,freq_table);
			System.out.println("starting correction");
			while(br.ready())
			{
				//test if we are getting a copy of br or if its counting up
				PacBioReadContainer read = new PacBioReadIntoContainer().readIntoContainer(br);
				kmerCorrection.correctReadByKmers(read);
				++counter;
				pw.println(read.toString(true));
//				System.out.println(read.toString());
				System.out.print(counter);
				//write it that read to a file
				pw.flush();
			}
			pw.close();
			pw = new PrintWriter(new BufferedWriter(new FileWriter(filenameStat))); //change to write the hole object!
			PacBioWriteStatistics stat_writer = new PacBioWriteStatistics();
			stat_writer.writeStats(stat, pw);
			pw.flush();
			pw.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
}
